home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SRC / WGTMENU.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  9.0 KB  |  408 lines

  1. #include <dos.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <wgt5.h>
  5.  
  6. /*
  7. m
  8. ╔═════════════════════════════════════════════════════════════════╗
  9. ║             █▓▒░ WordUp Graphics Toolkit V5.0 ░▒▓█              ║
  10. ║         Source Code    Copyright 1995 Egerter Software          ║
  11. ╟─────────────────────────────────────────────────────────────────╢
  12. ║ Module:       wgtmenu.c                                         ║
  13. ║ Contains:     showmenubar, removemenubar, initdropdowns,        ║
  14. ║               checkmenu                                         ║
  15. ║                                                                 ║
  16. ║ Last Revised: April 25, 1995                                    ║
  17. ║                                                                 ║
  18. ║ Written by:   Chris Egerter            WATCOM PROTECTED MODE!   ║
  19. ╚═════════════════════════════════════════════════════════════════╝
  20. */
  21.  
  22. static struct {
  23.   char patch_id[8];
  24.   char fname[12];
  25.   float version;
  26. } patch_struct = {"WGTPATCH", "wgtmenu", 1.0} ;
  27.  
  28. block menubarsave = NULL;       /* saves area at top of screen where
  29.                    menu bar will go*/
  30.  
  31. block dropsave = NULL;          /* saves area where drop down menus go */
  32.  
  33. short curchoice;
  34. short oldy;       
  35. extern char *menubar[10];       /* your menubar structure */
  36.  
  37. typedef struct {
  38.     char  choice[10][30];
  39.     short menux,menuy;
  40.     short color;
  41.     short bordercolor;
  42.     short textcolor;
  43.     } menulist; 
  44.  
  45. menulist dropdown[10];          /* the drop down list */
  46.  
  47. void initdropdowns (void);       
  48. void removemenubar (void);
  49. void showmenubar (void);
  50. short  checkmenu (void);
  51. void displaydrop (short, short);    /* show the drop down menu */
  52. void menusetto (short, short);      /* go to drop down (keyboard mode) */
  53. void wmouseon (void);
  54. void wmouseoff (void);          /* mouse on and off with installed checked */
  55.  
  56. short menubarcolor;
  57. short menubartextcolor;
  58. short bordercolor;
  59.  
  60. short     menubarheight;
  61. short     menubarlengths[10];
  62. wgtfont menufont;
  63. short     olddropx;
  64. short     highlightcolor;
  65. static short px;                  /* Local mouse variables */
  66. static short py;
  67. static short pbut;
  68. short     mouseinstalled = 0;
  69. short     currentdropdown = 11;
  70. char    menuhotkey = 0x44;      /* F10 */
  71.  
  72.  
  73. void menustart (void)
  74. {
  75.   /* set up of defaults */
  76.   short i;
  77.   short i2;
  78.  
  79.   for (i = 0; i < 10; i++)
  80.     for (i2 = 0; i2 < 10; i2++)
  81.     {
  82.       dropdown[i].color = 254;
  83.       dropdown[i].bordercolor = 255;
  84.       dropdown[i].textcolor = 1;
  85.     }
  86.   i = minit ();
  87.   if (i > 0)
  88.     mouseinstalled = 1;
  89.   else mouseinstalled = 0;
  90.   mouse.mx=WGT_SYS.xres - 1; mouse.my=WGT_SYS.yres - 1; mouse.but=0;
  91. }
  92.  
  93.  
  94.  
  95. void showmenubar (void)
  96. {
  97.   short mh;
  98.   short th;
  99.   short i;
  100.  
  101.   mh = 0;
  102.   for (i = 0; i < 10; i++)
  103.   {
  104.     if (menubar[i] != NULL) 
  105.     {
  106.       th = wgettextheight (menubar[i], menufont);
  107.       if (th > mh) 
  108.     mh = th;
  109.     }
  110.   }
  111.   menubarheight = mh + 2;
  112.   if (menubarsave != NULL)
  113.     wfreeblock (menubarsave);
  114.   menubarsave = wnewblock (0, 0, WGT_SYS.xres - 1, mh + 2);
  115.  
  116.   wsetcolor (menubarcolor);
  117.   wbar (0, 0, WGT_SYS.xres - 1,mh + 2);
  118.   wsetcolor (bordercolor);
  119.   wline (0, mh + 2, WGT_SYS.xres - 1, mh + 2);
  120.   wsetcolor (menubartextcolor);
  121.   wtexttransparent (0);
  122.   th = 0;
  123.  
  124.   /* display the menu bar titles */
  125.   for (i = 0; i < 10; i++)
  126.   {
  127.     if (menubar[i] != NULL)
  128.     {
  129.       wouttextxy (th, 1, menufont, menubar[i]);
  130.       menubarlengths[i] = wgettextwidth (menubar[i], menufont);
  131.       th += menubarlengths[i];
  132.     }
  133.   }
  134. }
  135.  
  136.  
  137.  
  138. void removemenubar (void)
  139. {
  140.   if (menubarsave != NULL)
  141.   {
  142.     wputblock(0, 0, menubarsave, 0);
  143.     wfreeblock (menubarsave);
  144.     menubarsave = NULL;
  145.   }
  146.   if (dropsave != NULL)
  147.   {
  148.     wfreeblock (dropsave);
  149.     dropsave = NULL;
  150.   }
  151. }
  152.  
  153.  
  154. short checkmenu(void)
  155. {
  156.   short  i;
  157.   short  j;
  158.   short  menuc;
  159.   char ink;
  160.   short  temp = 0;
  161.  
  162.   menuc =- 1;
  163.   if (mouseinstalled == 1) 
  164.   {
  165.     px = mouse.mx;
  166.     py = mouse.my;
  167.     pbut = mouse.but;
  168.   }
  169.   else if (kbhit ())
  170.   {
  171.     ink = getch ();
  172.     if (ink == 0)
  173.       ink = getch ();
  174.     if (ink == menuhotkey)
  175.       menusetto (0, 20);
  176.     if ((ink == 77) & (currentdropdown != 11))
  177.       menusetto (currentdropdown + 1, 20);
  178.     if ((ink == 75) & (currentdropdown != 11))
  179.       menusetto (currentdropdown - 1, 20);
  180.     if ((ink == 72) & (currentdropdown != 11))
  181.       menusetto (currentdropdown, curchoice - 1);
  182.     if ((ink == 80) & (currentdropdown != 11))
  183.       menusetto (currentdropdown, curchoice + 1);
  184.     if (ink == 13)      /* return */
  185.       mouse.but = 1;
  186.     if (ink == 27)      /* ESC */
  187.     { 
  188.       mouse.mx=WGT_SYS.xres - 1; mouse.my=WGT_SYS.yres - 1; 
  189.     }
  190.   }
  191.  
  192.   if (currentdropdown != 11)
  193.   {
  194.     if ((px >= olddropx) & (px < olddropx + dropdown[currentdropdown].menux)
  195.       && (py < dropdown[currentdropdown].menuy + menubarheight + 3))
  196.     {
  197.       temp = menubarheight + 5;
  198.       for (j = 0; j < 10; j++)
  199.       {
  200.     if ((dropdown[currentdropdown].choice[j][0] != 0) && (py >= temp))
  201.     { 
  202.       i = temp;
  203.       temp += wgettextheight (dropdown[currentdropdown].choice[j], menufont) + 2;
  204.       if (py < temp)
  205.       {
  206.         if (curchoice != j)
  207.         {
  208.           if (curchoice != -1)      /* erase old */
  209.           {
  210.         wmouseoff ();
  211.         wtextcolor (dropdown[currentdropdown].textcolor);
  212.         wouttextxy (olddropx + 4, oldy, menufont, dropdown[currentdropdown].choice[curchoice]);
  213.         wmouseon ();
  214.           }
  215.           wmouseoff ();
  216.           wtextcolor (highlightcolor);
  217.           wouttextxy (olddropx + 4, i, menufont, dropdown[currentdropdown].choice[j]);
  218.           wmouseon ();
  219.           curchoice = j;
  220.           oldy = i;
  221.         }
  222.         if (mouse.but != 0)
  223.         {
  224.           menuc = currentdropdown * 10 + j;
  225.           wmouseoff ();
  226.           wputblock (olddropx, menubarheight + 1, dropsave, 0);
  227.           wfreeblock (dropsave);
  228.           dropsave = NULL;
  229.           currentdropdown = 11;
  230.           wmouseon ();
  231.           mouse.but = 0;
  232.         }
  233.       }
  234.     }
  235.       }
  236.     }
  237.       else 
  238.     {
  239.       wmouseoff ();
  240.       if (dropsave != NULL)
  241.       {
  242.     wputblock (olddropx, menubarheight + 1, dropsave, 0);
  243.     wfreeblock (dropsave);
  244.     dropsave = NULL;
  245.       }
  246.       currentdropdown = 11;
  247.       wmouseon ();
  248.     }
  249.   }
  250.   temp = 0;
  251.   if (py <= menubarheight)
  252.   {
  253.     for (i = 0; i < 10; i++)
  254.     {
  255.       if (menubar[i] != NULL)
  256.       {
  257.     if ((px >= temp) && (px < temp + menubarlengths[i]))
  258.     {
  259.       if (currentdropdown != i)
  260.         displaydrop (i, temp);
  261.     }
  262.     temp += menubarlengths[i];
  263.       }
  264.     }
  265.   }
  266.   return menuc;
  267. }
  268.  
  269.  
  270.  
  271. void initdropdowns(void)
  272. {
  273.   short i;
  274.   short j;
  275.   short mew;
  276.   short meh;
  277.   short temp;
  278.  
  279.   menustart ();
  280.   for (i = 0; i < 10; i++)
  281.   {
  282.     mew = 0;
  283.     meh = 0;
  284.     for (j = 0; j < 10; j++)
  285.     {
  286.       temp = wgettextwidth (dropdown[i].choice[j], menufont);
  287.       if (temp > mew) 
  288.     mew = temp;
  289.       temp = wgettextheight (dropdown[i].choice[j], menufont);
  290.       meh += temp;
  291.       if (dropdown[i].choice[j] != NULL)
  292.     meh += 2;
  293.     }
  294.     meh += 10;
  295.     mew += 10;
  296.     dropdown[i].menux = mew;
  297.     dropdown[i].menuy = meh;
  298.   }
  299. }
  300.  
  301.  
  302.  
  303. void displaydrop (short menudrop, short startat)
  304. {
  305.   short dat;
  306.   short j;
  307.   short x1;
  308.   short y1;
  309.   short x2;
  310.   short y2;
  311.  
  312.   currentdropdown = menudrop;
  313.   curchoice =- 1;
  314.   wmouseoff ();
  315.   if (dropsave != NULL)
  316.   {
  317.     wputblock (olddropx, menubarheight + 1, dropsave, 0);
  318.     wfreeblock (dropsave);
  319.   }
  320.  
  321.   if (startat + dropdown[menudrop].menux > WGT_SYS.xres - 1)
  322.     dat = (WGT_SYS.xres - 1) -dropdown[menudrop].menux;
  323.   else dat = startat;
  324.  
  325.   x1 = dat;
  326.   y1 = menubarheight + 1;
  327.   x2 = dat + dropdown[menudrop].menux;
  328.   y2 = menubarheight + 1 + dropdown[menudrop].menuy;
  329.   olddropx = dat;
  330.  
  331.   dropsave = wnewblock (x1, y1, x2, y2);
  332.   wsetcolor (dropdown[menudrop].color);
  333.   wbar (x1, y1, x2, y2);
  334.   wsetcolor (dropdown[menudrop].bordercolor);
  335.   wrectangle (x1, y1, x2, y2);
  336.  
  337.   dat = y1 + 4;
  338.   for (j = 0; j < 10; j++)
  339.   {
  340.     if (dropdown[menudrop].choice[j][0] != 0)
  341.     {
  342.       wtextcolor (dropdown[menudrop].textcolor);
  343.       wouttextxy (olddropx + 4, dat, menufont, dropdown[menudrop].choice[j]);
  344.       dat += wgettextheight (dropdown[menudrop].choice[j],menufont) + 2;
  345.     }
  346.   }
  347.   wmouseon ();
  348. }
  349.  
  350.  
  351.  
  352. void menusetto (short menu, short choice)
  353. {
  354.   short i;
  355.   short j;
  356.   short  temp = 0;
  357.  
  358.   if (menu < 0)
  359.     menu = 0;
  360.   if (menu > 9)
  361.     menu = 9;
  362.  
  363.   if (choice < 0)
  364.     choice = 0;
  365.   if ((choice > 9) && (choice != 20))
  366.     choice = 9;
  367.  
  368.   /* set the menu */
  369.   for (i = 0; i < 10; i++)
  370.   {
  371.     if ((menubar[i] != NULL) && (i == menu))
  372.     {
  373.       px = temp;
  374.       py = 0;
  375.       if (currentdropdown != i)
  376.       curchoice =- 1;   
  377.     }
  378.     temp += menubarlengths[i];
  379.   }
  380.  
  381.   /* set the choice */
  382.   if (choice != 20)
  383.   {
  384.     temp = menubarheight + 6;
  385.     for (j = 0; j < 10; j++)
  386.     {
  387.       if (choice == j)
  388.     py = temp;
  389.       temp += wgettextheight (dropdown[currentdropdown].choice[j], menufont) + 2;
  390.     }
  391.   }
  392. }
  393.  
  394.  
  395.  
  396. void wmouseon (void)
  397. {
  398.   if (mouseinstalled == 1)
  399.   mon ();
  400. }
  401.  
  402.  
  403. void wmouseoff (void)
  404. {
  405.   if (mouseinstalled == 1)
  406.   moff ();
  407. }
  408.